<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default.html(bodyClass='pear-container')}">
<head>
    <title>用户管理</title>
</head>
<body>
<section layout:fragment="wrapper">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">编号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="no" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">性别</label>
                        <div class="layui-input-inline">
                            <select name="sex" class="layui-select">
                                <option value="">全部</option>
                                <option value="1">男</option>
                                <option value="2">女</option>
                                <option value="3">其他</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">身份证号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="idCard" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">单位</label>
                        <div class="layui-input-inline">
                            <input type="text" name="officeId" class="layui-hide">
                            <ul id="officeTree" class="dtree" data-id=""></ul>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="status" class="layui-select">
                                <option value="">全部</option>
                                <option value="1">启用</option>
                                <option value="2">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="tb-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="tb-table" lay-filter="tb-table"></table>
        </div>
    </div>

    <script type="text/html" id="tb-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </script>

    <script type="text/html" id="tb-bar">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
            <i class="layui-icon layui-icon-delete"></i>
        </button>
    </script>

    <script type="text/html" id="tb-sex">
        {{#if (d.sex == '1') { }}
        <span>男</span>
        {{# }else if(d.sex == '2'){ }}
        <span>女</span>
        {{# }else if(d.sex == '3'){ }}
        <span>其他</span>
        {{# } }}
    </script>

    <script type="text/html" id="tb-status">
        {{#if (d.status == '1') { }}
        <span class="layui-badge layui-bg-green">启用</span>
        {{# }else if(d.status == '2'){ }}
        <span class="layui-badge layui-bg-gray">禁用</span>
        {{# } }}
    </script>

    <script th:inline="javascript">
        layui.use(['table', 'form', 'jquery', 'dtree'], function () {
            let table = layui.table;
            let form = layui.form;
            let dtree = layui.dtree;
            let $ = layui.jquery;

            dtree.render({
                elem: "#officeTree",
                initLevel: "1",
                line: true,
                method: 'post',
                url: '[(@{/a/sys/office/data})]',
                dataFormat: "list",
                dataStyle: "layuiStyle",
                response: {message: "msg", statusCode: '20000'},
                select: true,
            });

            dtree.on("node('officeTree')" ,function(obj){
                $('input[name="officeId"]').val(obj.param.nodeId);
            });

            table.render({
                elem: '#tb-table',
                url: '[(@{/a/sys/user/page})]',
                method: 'post',
                response: {
                    statusCode: '20000',
                },
                page: true,
                cols: [
                    [
                        {type: 'checkbox'},
                        {title: '用户名', field: 'username', align: 'center'},
                        {title: '姓名', field: 'name', align: 'center'},
                        {title: '编号', field: 'no', align: 'center'},
                        {title: '性别', field: 'sex', align: 'center', templet: '#tb-sex', width: 80},
                        {title: '身份证号', field: 'idCard', align: 'center'},
                        {title: '出生日期', field: 'birthday', align: 'center'},
                        {title: '单位', field: 'officeTitle', align: 'center'},
                        {title: '邮箱', field: 'email', align: 'center', hide: true},
                        {title: '电话', field: 'phone', align: 'center', hide: true},
                        {title: '手机', field: 'mobile', align: 'center', hide: true},
                        {title: '入职日期', field: 'entryDate', align: 'center', hide: true},
                        {title: '状态', field: 'status', align: 'center', templet: '#tb-status', width: 80},
                        {title: '操作', toolbar: '#tb-bar', align: 'center'}
                    ]
                ],
                skin: 'line',
                toolbar: '#tb-toolbar',
                defaultToolbar: [
                    {title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh'},
                    'filter', 'exports', 'print'
                ]
            });

            table.on('tool(tb-table)', function (obj) {
                if (obj.event === 'remove') {
                    remove(obj);
                } else if (obj.event === 'edit') {
                    edit(obj);
                }
            });

            table.on('toolbar(tb-table)', function (obj) {
                if (obj.event === 'add') {
                    add();
                } else if (obj.event === 'refresh') {
                    refresh();
                } else if (obj.event === 'batchRemove') {
                    batchRemove(obj);
                }
            });

            form.on('submit(tb-query)', function (data) {
                table.reload('tb-table', {
                    where: data.field,
                    page: {
                        curr: 1
                    }
                })
                return false;
            });

            const add = function () {
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/a/sys/user/form})]'
                });
            }

            const edit = function (obj) {
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    maxmin: true,
                    area: ['900px', '600px'],
                    content: '[(@{/a/sys/user/form})]?id=' + obj.data['id']
                });
            }

            const remove = function (obj) {
                removeData([obj.data['id']]);
            }

            const batchRemove = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }
                let ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].id);
                }
                removeData(ids);
            }

            const removeData = function (array) {
                layer.confirm('确定要删除数据', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: '[(@{/a/sys/user/del})]',
                        data: {datas: array},
                        dataType: 'json',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code === '20000') {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    refresh()
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            const refresh = function () {
                table.reload('tb-table');
            }
        })
    </script>
</section>
</body>
</html>
